var MJModel           = require('./MJModel');
let roomAPI           = require('../../API/RoomAPI');
var RoomProto         = require('../../API/RoomProto');
var MJProto           = require('./MJProto');

cc.Class({
	extends: cc.Component,
	properties: {
		cardsLayout: cc.Node,
		restLayout: cc.Node,
		itemNode: cc.Node,
		cardNode: cc.Node,
		clockLabel: cc.Label,
		majiangAltar: cc.SpriteAtlas,
	},

	start: function () {
		this.maNodes = [];
		this.node.active = false;
		this.resultData = this.dialogParameters.result;
		this.scheduleOnce(() => {
			this.node.active = true;
		}, 3);
		this.tick = 30;
		this.setAllHandCards();
		if (this.dialogParameters.isBureauFull) {
			cc.find('btnNode', this.node).active = false;
			cc.find('ContinueBtn', this.node).active = true;
		}
	},

	setAllHandCards: function () {
		let posy = 350, itemHeight = 130;
		posy -= itemHeight/2;
		for (let i = 0; i < MJModel.getChairCount(); ++i) {
			if (!this.resultData.handCards[i]) {
				continue;
			}
			let item = this.getHandCardsItem(i);
			item.parent = this.node;
			item.active = true;
			item.y = posy;
			posy -= itemHeight;
		}
		posy += 60;

		/* 码牌 */
		let myMaCards = this.resultData.myMaCards;
		if (myMaCards && myMaCards.length > 0) {
			posy -= 20;
			let maLabel = cc.find('MaLabel', this.node);
			maLabel.active = true;
			maLabel.y = posy;
			posy += 20;
			let maLayout = cc.find('ZhongMa', this.node);
			maLayout.active = true;
			maLayout.y = posy;
			for (let i = 0; i < myMaCards.length; ++i) { /*马牌*/ 
				let node = this.getCardNode(myMaCards[i].card);
				cc.find('choosed', node).active = myMaCards[i].win;
				node.parent = maLayout;
				node.x = 18+37*i;
			}
			posy -= 70;
		}

		/* 剩余的牌 */
		posy -= 20;
		let restLabel = cc.find('RestLabel', this.node);
		restLabel.y = posy;
		this.restLayout.y = posy + 20;
		let restCards = this.resultData.restCards;
		for (let i = 0; i < restCards.length; ++i) {
			this.getCardNode(restCards[i]).parent = this.restLayout;
		}
	},

	getHandCardsItem: function (chairID) {
		let item = cc.instantiate(this.itemNode);
		let user = MJModel.getUserByChairID(chairID);
		if (user) {
			cc.find('name', item).getComponent(cc.Label).string = user.userInfo.nickname;
			cc.find('uid', item).getComponent(cc.Label).string = user.userInfo.uid;
			cc.find('owner', item).active = (user.chairID == MJModel.getRoomCreatorChairID());
			Global.CCHelper.updateSpriteFrame(user.userInfo.avatar, cc.find('head', item).getComponent(cc.Sprite));
			cc.find('banker', item).active = (user.chairID == MJModel.getBankerChairID());
		}
		let layout = cc.find('Layout', item);
		let posx = 0, cardWidth = 37, nullWidth = 10;
		posx += cardWidth/2;
		let pengGangArray = MJModel.getPengGangArrayByChairID(chairID);
		if (pengGangArray.length > 0) {
			for (let i = 0; i < pengGangArray.length; ++i) {
				let obj = pengGangArray[i];
				let count = (MJProto.operateType.PENG == obj.operate)? 3:4;
				let sumx = 0;
				for (let j = 0; j < count; ++j) {
					let card = (obj.operate == MJProto.operateType.GANG_ZHI && j < 3)? 0:obj.card;
					let node = this.getCardNode(card);
					node.parent = layout;
					if (count == 4 && j == 3) {
						node.y = 10;
						node.x = posx-2*cardWidth;
					}
					else {
						node.x = posx;
						posx += cardWidth;
					}
					sumx += node.x;
				}
				if (count == 4) {
					let label = cc.instantiate(cc.find('malabel', item));
					label.active = true;
					label.parent = item;
					label.x = sumx/4+layout.x;
					if (obj.operate == MJProto.operateType.GANG_ZHI) { /* 暗杠 */ 
						label.getComponent(cc.Label).string = '暗杠';
					}
					else if (obj.operate == MJProto.operateType.GANG_BU) { /* 明杠 */ 
						label.getComponent(cc.Label).string = '明杠';
					}
					else if (obj.operate == MJProto.operateType.GANG_CHI) { /* 接杠 */ 
						label.getComponent(cc.Label).string = '接杠';
					}
				}
				posx += nullWidth;
			}
		}

		// let handCards = MJModel.getHandCardsByChairID(chairID);
		let handCards = this.resultData.handCards[chairID];
		let fangGangCount = 0;
		if (this.resultData.fangGangArray && this.resultData.fangGangArray.length > 0) {
			for (let i = 0; i < this.resultData.fangGangArray.length; ++i) {
				if (this.resultData.fangGangArray[i] == chairID) {
					++ fangGangCount;
				}
			}
		}
		if (this.resultData.winChairIDArray.indexOf(chairID) != -1) {
			cc.find('icon', item).active = true;
			for (let i = 0; i < handCards.length-1; ++i) {
				let node = this.getCardNode(handCards[i]);
				node.parent = layout;
				node.x = posx;
				posx += cardWidth;
				if (fangGangCount > 0 && i == handCards.length-2) {
					let label = cc.instantiate(cc.find('malabel', item));
					label.parent = item;
					label.active = true;
					label.getComponent(cc.Label).string = (fangGangCount == 1)? '放杠':'放杠x'+fangGangCount;
					label.x = node.x+layout.x;
				}
			}
			posx += nullWidth;
			let huCardNode = this.getCardNode(handCards[handCards.length-1]);
			huCardNode.parent = layout;
			huCardNode.x = posx;
			posx += cardWidth;
			let huNode = cc.find('hu', item);
			huNode.parent = huCardNode;
			huNode.active = true;
			huNode.setPosition(18, 25);
			posx += nullWidth;
			let resultLabel = cc.find('resultLabel', item);
			resultLabel.active = true;
			resultLabel.getComponent(cc.Label).string = (this.resultData.huType == MJProto.operateType.HU_ZHI)? '自摸':'抢杠';
		}
		else {
			for (let i = 0; i < handCards.length; ++i) {
				let node = this.getCardNode(handCards[i]);
				node.parent = layout;
				node.x = posx;
				posx += cardWidth;
				if (fangGangCount > 0 && i == handCards.length-1) {
					let label = cc.instantiate(cc.find('malabel', item));
					label.parent = item;
					label.active = true;
					label.getComponent(cc.Label).string = (fangGangCount == 1)? '放杠':'放杠x'+fangGangCount;
					label.x = node.x+layout.x;
				}
			}
		}

		let score = this.resultData.scores[chairID];
		if (score > 0) {
			let label = cc.find('win', item).getComponent(cc.Label);
			label.node.active = true;
			label.string = '+' + Math.floor(score);
		}
		else {
			let label = cc.find('lose', item).getComponent(cc.Label);
			label.node.active = true;
			label.string = Math.floor(score);
		}
		return item;
	},

	setRestCards: function (posy) {
	},

	update: function (dt) {
		this.tick -= dt;
		let tick = Math.floor(this.tick);
		if (tick > 0) {
			this.clockLabel.string = tick;
		}
		else {
			this.clockLabel.string = '0';
			Global.DialogManager.destroyDialog('Game/Majiang/MJResultDialog');
		}
	},

    onBtnClick: function (event, parameters) {
		Global.AudioManager.playCommonSoundClickButton();
		if(parameters === 'ready'){
			if (!this.dialogParameters.isBureauFull) {
				roomAPI.roomMessageNotify(RoomProto.userReadyNotify());
			}
			else {
				if (this.dialogParameters.mjMainDialog.showMJAllResultFunc) {
					this.dialogParameters.mjMainDialog.showMJAllResultFunc();
				}
			}
			Global.DialogManager.destroyDialog('Game/Majiang/MJResultDialog');
		}
		else if(parameters === 'exit'){
			if (this.dialogParameters.mjMainDialog.showMJAllResultFunc) {
				this.dialogParameters.mjMainDialog.showMJAllResultFunc();
			}
			Global.DialogManager.destroyDialog('Game/Majiang/MJResultDialog');
		}
	},

	getCardNode: function (card) {
		let node = cc.instantiate(this.cardNode);
		card = (card <= 9)? '0'+card:''+card;
		node.active = true;
		node.y = 0;
		let name = "down2_"+Math.floor(card/10)+"_"+(card%10);
		node.getComponent(cc.Sprite).spriteFrame = this.majiangAltar.getSpriteFrame(name);
		return node;
	},

	getNullNode: function () {
		let node = new cc.Node();
		node.width = 20;
		return node;
	},
});
